package com.gmgx.hrms.dao.impl;

import com.gmgx.hrms.dao.DeptDao;
import com.gmgx.hrms.dao.EmpDao;
import com.gmgx.hrms.dao.RoleDao;
import com.gmgx.hrms.dao.TransferDao;
import com.gmgx.hrms.entity.Dept;
import com.gmgx.hrms.entity.Emp;
import com.gmgx.hrms.entity.Role;
import com.gmgx.hrms.entity.Transfer;
import com.gmgx.hrms.util.JDBCTools;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class TransferDaoImpl implements TransferDao {
    private JDBCTools tools=new JDBCTools();
    @Override
    public boolean queryTransferByEmpid(int empid) {
        boolean flag=false;
       ResultSet rs= tools.query("select * from t_transfer where transfer_emp=? and transfer_status!=3 and transfer_status!=5",empid);
        try {
            if(rs.next()){
                flag= true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            tools.clossAll();
        }
        return flag;
    }

    @Override
    public boolean addTransferForEmp(Transfer transfer) {
       int  i=  tools.update("insert into t_transfer(transfer_emp,transfer_role,transfer_dept,transfer_reason,transfer_status,transfer_time) values(?,?,?,?,?,?)",
               transfer.getTransfer_emp().getEmp_id(),transfer.getTransfer_role().getRole_id(),transfer.getTransfer_dept().getDept_id(),transfer.getTransfer_reason(),transfer.getTransfer_status(),transfer.getTransfer_time() );
        return i>0;
    }

    @Override
    public List<Transfer> queryTransferByDeptidForManager(int deptid) {
        List<Transfer> list=new ArrayList<>();
        ResultSet rs=tools.query("select * from t_transfer inner join t_emp on transfer_emp=emp_id where emp_dept=? and transfer_status='0'",deptid);
        try {
            while(rs.next()){
                Transfer transfer=new Transfer();
                transfer.setTransfer_id(rs.getInt("transfer_id"));
                transfer.setTransfer_status(rs.getString("transfer_status"));
                transfer.setTransfer_time(rs.getDate("transfer_time").toLocalDate());
                transfer.setTransfer_reason(rs.getString("transfer_reason"));
                int empid=rs.getInt("transfer_emp");
                EmpDao empDao=new EmpDaoImpl();
                Emp emp= empDao.queryEmpById(empid);
                transfer.setTransfer_emp(emp);

                RoleDao roleDao=new RoleDaoImpl();
                Role role=   roleDao.queryRoleById(rs.getInt("transfer_role"));
                transfer.setTransfer_role(role);

                DeptDao deptDao=new DeptDaoImpl();
                Dept dept=deptDao.queryDeptById(rs.getInt("transfer_dept"));
                transfer.setTransfer_dept(dept);

                list.add(transfer);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            tools.clossAll();
        }

        return list;
    }

    @Override
    public boolean updateTransferByTransferidForManager(int transferid, String status) {
        return tools.update("update t_transfer set transfer_status=? where transfer_id=?",status,transferid)>0;
    }
}
